package com.example.service;

import com.example.entity.Score;
import com.example.entity.StudyAreaTime;
import com.example.mapper.StudyAreaTimeMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/*
帖子业务处理
 */
@Service
public class StudyAreaTimeService {

    @Resource
    private StudyAreaTimeMapper studyAreaTimeMapper;

    public void add(StudyAreaTime studyAreaTime) {
        studyAreaTimeMapper.insert(studyAreaTime);
    }
    public void updateByCondition(StudyAreaTime studyAreaTime) {
        studyAreaTimeMapper.updateByCondition(studyAreaTime);
    }


    public void deleteById(Integer id) {
        studyAreaTimeMapper.deleteById(id);
    }

    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids){
            this.deleteById(id);
        }
    }

    public void updateById(StudyAreaTime studyAreaTime) {
        List<StudyAreaTime> theStudyAreaTime=selectAllByUidOrCreateTime(studyAreaTime);
        Field nameField = null;
        boolean flag=true;
        if(studyAreaTime.getId()==null)
        {
            flag=false;
        }
        /**倘若没有找到执行添加*/
        if(theStudyAreaTime.size()==0)
        {
            add(studyAreaTime);
            return;
        }
        /**属性名列表*/
        List<String> attribute= new ArrayList<>();
        attribute.add("mapTime");
        attribute.add("aiTime");
        attribute.add("checkPointTime");
        attribute.add("lookVideo");
        attribute.add("playPet");
        String newValue=null;
        String oldValue=null;
        for(String item:attribute)
        {
            try {
                nameField=StudyAreaTime.class.getDeclaredField(item);
                nameField.setAccessible(true);
                newValue= String.valueOf(nameField.get(studyAreaTime));
                oldValue=String.valueOf(nameField.get(theStudyAreaTime.get(0)));
                if(oldValue.equals("null"))
                {
                    oldValue="0";
                }
                System.out.println("得到"+newValue);
                nameField.set(theStudyAreaTime.get(0),String.valueOf(Integer.valueOf(oldValue)+Integer.valueOf(newValue)));
            } catch (Exception e) {
                //
            }
        }
        if(!flag)
        {
            studyAreaTimeMapper.updateById(theStudyAreaTime.get(0));
        }
        else
        {
            System.out.println("使用默认");
            studyAreaTimeMapper.updateById(studyAreaTime);
        }
    }

    public StudyAreaTime selectById(Integer id) {
        return studyAreaTimeMapper.selectById(id);
    }

    public List<StudyAreaTime> selectAll(StudyAreaTime studyAreaTime) {
        return studyAreaTimeMapper.selectAll(studyAreaTime);
    }
    public List<StudyAreaTime> selectAllByCondition(StudyAreaTime studyAreaTime) {
        return studyAreaTimeMapper.selectAllByCondition(studyAreaTime);
    }


    public PageInfo<StudyAreaTime> selectPage(StudyAreaTime studyAreaTime, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<StudyAreaTime> studyAreaTimeList = studyAreaTimeMapper.selectAll(studyAreaTime);
        return PageInfo.of(studyAreaTimeList);
    }
    /**仅通过uid或者id或者createTime来查询*/
    public List<StudyAreaTime> selectAllByUidOrCreateTime(StudyAreaTime studyAreaTime)
    {
        return studyAreaTimeMapper.selectAllByUidOrCreateTime(studyAreaTime);
    }

}
